<template>

    <div>
        <el-dialog
            @close="ConfirmClose"
            :before-close="handleClose"
            title="Edit Report"
            :visible.sync="visible">



            <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px"
                     label-position="left">
                <el-row type="flex" justify="start" align="top">
                    <el-form-item label="reportId" label-width="180px" prop="reportId">
                        <el-input v-model="formData.reportId" :disabled='true' clearable :style="{width: '100%'}">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="reportDept" label-width="180px" prop="reportDept">
                        <el-input v-model="formData.reportDept" :disabled='true' clearable
                                  :style="{width: '100%'}"></el-input>
                    </el-form-item>
                </el-row>
                <el-row type="flex" justify="start" align="top">
                    <el-form-item label="customerMgrId" label-width="180px" prop="customerMgrId">
                        <el-input v-model="formData.customerMgrId" :disabled='true' clearable :style="{width: '100%'}">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="customerMgrName" label-width="180px" prop="customerMgrName">
                        <el-input v-model="formData.customerMgrName" :disabled='true' clearable
                                  :style="{width: '100%'}"></el-input>
                    </el-form-item>
                </el-row>



                <el-row type="flex" justify="start" align="top">


                    <el-form-item label="reportDate" label-width="180px" prop="reportDate">
                        <el-date-picker v-model="formData.reportDate" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                                        :style="{width: '100%'}" placeholder="Y-M-D" clearable></el-date-picker>
                    </el-form-item>

                    <el-form-item label="week" label-width="165px" prop="reportWeek">
                        <el-select v-model="formData.reportWeek" placeholder="Please choose week" clearable
                                   :style="{width: '100%'}">
                            <el-option v-for="(item, index) in options.reportWeekOptions" :key="index" :label="item.label"
                                       :value="item.value" :disabled="item.disabled"></el-option>
                        </el-select>
                    </el-form-item>
                </el-row>
                <el-row type="flex" justify="start" align="top">


                    <el-form-item label-width="180px" label="reportContent" prop="reportContent">
                        <el-input v-model="formData.reportContent" type="textarea" placeholder="Please enter reportContent"
                                  :autosize="{minRows: 2, maxRows: 5}" :style="{width: '200%'}"></el-input>
                    </el-form-item>

                </el-row>

            </el-form>

            <div slot="footer">
                <el-button @click="ConfirmClose">取消</el-button>
                <el-button type="primary" @click="handleConfirm">确定</el-button>
            </div>



        </el-dialog>
    </div>

</template>

<script>
import {options} from "@/data/const"
export default {
    name: "EditReportDialog",
    props: ['visible', 'close', 'callback'],


    data() {
        return {
            //从父组件获得这一行数据的index，方便之后将数据修改传回去
            options: options,
            index: null,
            formData: {
                reportId: null,
                reportDept: 'xxx',
                customerMgrId: 1,
                customerMgrName: 'xxx',

                reportDate: '',
                reportWeek: '',
                reportContent: '',
            },

            rules: {
                reportId: [{
                    required: true,
                    message: '',
                    trigger: 'blur'
                }],
                reportDept: [{
                    required: true,
                    message: '',
                    trigger: 'blur'
                }],
                customerMgrId: [{
                    required: true,
                    message: '',
                    trigger: 'blur'
                }],
                customerMgrName: [{
                    required: true,
                    message: '',
                    trigger: 'blur'
                }],
                reportDate: [{
                    required: true,
                    message: 'Please enter report date',
                    trigger: 'blur'
                }],
                reportWeek: [{
                    required: true,
                    message: 'Please enter report week',
                    trigger: 'change'
                }],
                reportContent: [{
                    required: true,
                    message: 'Please enter report content',
                    trigger: 'change'
                }],
            },


        }
    },

    methods: {

        //弹窗关闭
        handleClose(done) {
            this.$confirm('现在关闭将丢失所有未保存数据，确认关闭？')
                // eslint-disable-next-line no-unused-vars
                .then(e => {
                    done();
                })
        },
        ConfirmClose() {
            this.close()
        },


        handleConfirm() {
            this.$refs['elForm'].validate(valid => {
                if (!valid) return

                console.log('before close', this.formData);
                //深拷贝
                const dd = JSON.parse(JSON.stringify(this.formData))

                this.$axios.post("/report/report/updateReport", this.formData).then(
                    e => {
                        if(e.data.status === 200){
                            this.$message.success(e.data.msg)
                        }
                    }
                )

                this.callback(dd,this.index)
                this.index=null

                this.$refs.elForm.resetFields()
                this.ConfirmClose();

            })
        },


        setData(data, index) {
            this.formData = data
            this.index = index
        },

    }

}
</script>

<style scoped>

</style>